package com.activenetwork.custom.uielement.decorate;

import java.lang.reflect.Field;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.support.pagefactory.ElementLocator;
import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.activenetwork.util.EnvContext;

public class XElementLocatorFactory implements ElementLocatorFactory {
	private static Logger logger = LoggerFactory.getLogger(XElementLocatorFactory.class); 
	
	/**
	 * Used by spring injection
	 * pay attention at time of injection, thread webdriver instance is NOT created
	 * so here we get NULL from thread local
	 */
	public XElementLocatorFactory(){}
	

	@Override
	public ElementLocator createLocator(Field field) {
		SearchContext searchContext = EnvContext.webDriverTL.get();
		try{
			return new XElementLocator(searchContext, field);
		}catch(RuntimeException e){
			logger.trace("Page:" + field.getDeclaringClass().getSimpleName() + " Field:" 
	    			+ field.getName() + " is not fit for XElementLocatorFactory");
			return null;
		}
	}
	


}
